CC = gcc
CFLAGS = -Wall -Wextra -g -O0
TARGET = debugger
TEST_TARGET = test_program

SOURCES = main.c debugger.c symbols.c dwarf.c dwarf_parser.c disasm_objdump.c dwarf_libdwarf.c
TEST_SOURCES = test_program.c
DWARF_TEST_SOURCES = test_dwarf.c dwarf.c dwarf_parser.c
ADVANCED_DWARF_TEST_SOURCES = test_advanced_dwarf.c debugger.c symbols.c dwarf.c dwarf_parser.c
INTEGRATION_TEST_SOURCES = test_dwarf_integration.c debugger.c symbols.c dwarf.c dwarf_parser.c

.PHONY: all clean test

all: $(TARGET)

$(TARGET): $(SOURCES)
	$(CC) $(CFLAGS) -I/usr/local/include/libdwarf-2 -o $(TARGET) $(SOURCES) -L/usr/local/lib -ldwarf

$(TEST_TARGET): $(TEST_SOURCES)
	$(CC) $(CFLAGS) -o $(TEST_TARGET) $(TEST_SOURCES)

test: $(TEST_TARGET) $(TARGET) test_dwarf test_advanced_dwarf
	@echo "编译测试程序..."
	@make $(TEST_TARGET)
	@echo "编译调试器..."
	@make $(TARGET)
	@echo "编译DWARF测试程序..."
	@make test_dwarf
	@echo "编译高级DWARF测试程序..."
	@make test_advanced_dwarf
	@echo "测试程序已编译为: $(TEST_TARGET)"
	@echo "调试器已编译为: $(TARGET)"
	@echo "DWARF测试程序已编译为: test_dwarf"
	@echo "高级DWARF测试程序已编译为: test_advanced_dwarf"
	@echo ""
	@echo "使用方法:"
	@echo "  1. 启动调试器: ./$(TARGET)"
	@echo "  2. 在调试器中输入: launch ./$(TEST_TARGET)"
	@echo "  3. 使用调试命令进行调试"
	@echo ""
	@echo "DWARF功能测试:"
	@echo "  ./test_dwarf"
	@echo "高级DWARF功能测试:"
	@echo "  ./test_advanced_dwarf ./$(TEST_TARGET)"

test_dwarf: $(DWARF_TEST_SOURCES)
	$(CC) $(CFLAGS) -o test_dwarf $(DWARF_TEST_SOURCES)

test_advanced_dwarf: $(ADVANCED_DWARF_TEST_SOURCES)
	$(CC) $(CFLAGS) -o test_advanced_dwarf $(ADVANCED_DWARF_TEST_SOURCES)

test_integration: $(INTEGRATION_TEST_SOURCES)
	$(CC) $(CFLAGS) -o test_integration $(INTEGRATION_TEST_SOURCES)

clean:
	rm -f $(TARGET) $(TEST_TARGET) test_dwarf test_advanced_dwarf test_integration *.o

install: $(TARGET)
	@echo "注意: 请确保有足够的权限进行安装"
	sudo cp $(TARGET) /usr/local/bin/
	@echo "调试器已安装到 /usr/local/bin/$(TARGET)"

uninstall:
	sudo rm -f /usr/local/bin/$(TARGET)
	@echo "调试器已卸载"